package com.ruoyi.gqgl.ipb.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gqgl.ipb.domain.Ipb;
import com.ruoyi.gqgl.ipb.mapper.IpbMapper;
import com.ruoyi.gqgl.ipb.service.IIpbService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * IPController
 *
 * @author huacai
 * @date 2025-01-21
 */
@RestController
@RequestMapping("/gqgl/ipb")
public class IpbController extends BaseController {
    @Resource
    private IIpbService ipbService;

    @Resource
    private IpbMapper ipbMapper;

    /**
     * 查询IP列表
     */
    @PreAuthorize("@ss.hasPermi('gqgl:ipb:list')")
    @GetMapping("/list")
    public TableDataInfo list(Ipb ipb) {
        startPage();
        List<Ipb> list = ipbService.selectIpbList(ipb);
        return getDataTable(list);
    }

    /**
     * 导出IP列表
     */
    @PreAuthorize("@ss.hasPermi('gqgl:ipb:export')")
    @Log(title = "IP", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Ipb ipb) {
        List<Ipb> list = ipbService.selectIpbList(ipb);
        ExcelUtil<Ipb> util = new ExcelUtil<Ipb>(Ipb.class);
        util.exportExcel(response, list, "IP数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Ipb> util = new ExcelUtil<Ipb>(Ipb.class);
        util.importTemplateExcel(response, "IP数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "IP", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('gqgl:ipb:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Ipb> util = new ExcelUtil<Ipb>(Ipb.class);
        InputStream inputStream = file.getInputStream();
        List<Ipb> list = util.importExcel(inputStream);
        inputStream.close();
        int count = ipbService.batchInsertIpb(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取IP详细信息
     */
    @PreAuthorize("@ss.hasPermi('gqgl:ipb:query')")
    @GetMapping(value = "/{ipbId}")
    public AjaxResult getInfo(@PathVariable("ipbId") String ipbId) {
        return success(ipbService.selectIpbByIpbId(ipbId));
    }

    /**
     * 新增IP
     */
    @PreAuthorize("@ss.hasPermi('gqgl:ipb:add')")
    @Log(title = "IP", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Ipb ipb) {

        //根据授权号/专利号和年度查询有没有重复的知识产权
        Boolean sfCz = ipbMapper.selectIpbAndSqhByrdNd(ipb.getRdNd(), ipb.getSqZlh());
        if (sfCz == true) {
            return error(ipb.getRdNd() + "认定年度中已存在该知识产权!");
        }

        return toAjax(ipbService.insertIpb(ipb));
    }

    /**
     * 修改IP
     */
    @PreAuthorize("@ss.hasPermi('gqgl:ipb:edit')")
    @Log(title = "IP", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Ipb ipb) {
        return toAjax(ipbService.updateIpb(ipb));
    }

    /**
     * 删除IP
     */
    @PreAuthorize("@ss.hasPermi('gqgl:ipb:remove')")
    @Log(title = "IP", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ipbIds}")
    public AjaxResult remove(@PathVariable String[] ipbIds) {
        return toAjax(ipbService.deleteIpbByIpbIds(ipbIds));
    }
}
